import { ref, reactive, onMounted, getCurrentInstance, onActivated, toRefs, watch } from 'vue';
import getTableData from '/@/utils/getTableData'


const getSelectRows = (postMethod: Function, props: any, searchObj: any = reactive({}), otherParams: any = {}) => {
    const state =  reactive({
        listData: [] as any[],
        selectedRows: [] as any[],
        status: '',
    })
    // 深拷贝
    const deepCopy = (data: any) => {
        let res: any = typeof data.splice === 'function' ? [] : {}
        if (typeof data === 'object') {
            for (let key in data) {
                if (data[key] && typeof data[key] === 'object') {
                    res[key] = deepCopy(data[key])
                } else {
                    res[key] = data[key]
                }
            }
        }
        return res
    }
    // table数据 
    const tableObj = getTableData(postMethod, otherParams, searchObj, false)
    
    const setSelectedStatus = () => {
        // 将原有选中的数据设置为选中状态
        if (state.selectedRows.length) {
            state.listData.forEach((item, index) => {
                let isExisted = state.selectedRows.some( idItem => idItem.id == item.id )
                if (isExisted) {
                    item.checked = true
                }
            })
        }
    }
    const checkboxHandle = (val: any, data: any) => {
        let marker = 'id'
        data.checked = val
        if (props.multiple) {
            // 先判断已选中是否已有该数据
            let isExisted = state.selectedRows.some( (rowItem: any)=> rowItem.id== data.id)
            if (isExisted) {
                if (!val) { //存在 并且取消，就删除
                    for( let i = 0; i < state.selectedRows.length; i++) {
                        let row = state.selectedRows[i]
                        if (row.id == data.id) {
                            state.selectedRows.splice(i, 1)
                            break;
                        }
                    }
                }
            } else {
                if (val) {
                    state.selectedRows.push(data)
                }
            }
        } else { //单选
            if (val == true) {
                state.selectedRows = []
                state.listData.forEach( (item: any) => {
                    if (item.id != data.id) {
                        item.checked = false
                    }
                })
                state.selectedRows = []
                state.selectedRows.push(data)

            }
        }
    }
    // 初始化
    const init = (selectedList: any[] = [], otherParams: any = {}) => {
        state.status = ''
        state.selectedRows = selectedList
        if (selectedList.length == 0) {
            tableObj.pageNo.value = 1
        }
        tableObj.getData(otherParams)
        return new Promise<any []>((resolve, reject) => {
            let timer = setInterval( () => {
                if (state.status == 'confirm') {
                    // console.log(state.selectPD)
                    clearInterval(timer)
                    console.log(state.selectedRows)
                    resolve(JSON.parse(JSON.stringify(state.selectedRows)))
                } else if (state.status == 'cancel') {
                    clearInterval(timer)
                    reject()
                }
            }, 100)
        })
    }
    // 盘点物料确认
    const confirm = () => {
        state.status = 'confirm'
    }
    // 取消
    const cancel = (list: any[]) => {
        state.status = 'cancel'
    }
    watch( ()=> tableObj.list.value, () => {
        let arr: any = deepCopy(tableObj.list.value)
        state.listData = arr.map ( (v: any) => {
            return {
                ...v,
                checked: false,
            }
        })
        setSelectedStatus()
    })
    return {
        ...toRefs(searchObj),
        ...toRefs(state),
        checkboxHandle,
        ...tableObj,
        cancel,
        confirm,
        init,
    }
}

export default getSelectRows;